import { useEffect, useRef, useState } from "react";

const useCountAuth = (num) => {
  const [count, setCount] = useState(num);
  const [isRun, setIsRun] = useState(false);
  const timer = useRef(null);
  const start = () => {
    setIsRun(true);
    timer.current = setInterval(() => {
      setCount((prve) => {
        const nCount = prve - 1;
        if (nCount <= 0) {
          clearInterval(timer.current);
          setIsRun(false);
          setCount(num);
        }
        return nCount;
      });
    }, 1000);
  };
  useEffect(() => {
    return () => {
      clearInterval(timer.current);
    };
  }, []);
  return { start, count, isRun };
};
export default useCountAuth;
